//
//  MyFavListController.m
//  QQplayground
//
//  Created by zhangxy on 2018/8/1.
//  Copyright © 2018年 zhang. All rights reserved.
//

#import "MyFavListController.h"
#import "ZZHomeListCell.h"
#define cellIdentifier @"ZZHomeListCell"
#import "ZZContentDetailController.h"
#import "ZZShareView.h"
#import "ZZContentCommentController.h"
#import "MyButton.h"
#import <MJRefresh.h>

@interface MyFavListController()<SDCycleScrollViewDelegate,UITableViewDelegate,UITableViewDataSource,ZZTopicCellDelegate>{
    int page;
}

@property(nonatomic,strong)  NSMutableArray *listArray;
@property(nonatomic,strong)  UITableView *listTable;

@property (nonatomic, assign) BOOL status;

@end

@implementation MyFavListController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    // Do any additional setup after loading the view.
    self.navigationItem.title = _entity.nav_name;
    
    [self setNavigationBar];
    
    self.navigationController.navigationBarHidden = NO;
    
    [self.view setBackgroundColor:UIColorFromRGB(BgSystemColor)];
    
    
    [self createTableView];
    
    [self loadBegin];
    
}

-(void)createTableView{
    _status = NO;
    
    _listArray = [[NSMutableArray alloc] init];
    
    _listTable=[self.view createTableView:self cell:cellIdentifier];
    [_listTable setFrame:self.view.bounds];
    
    [_listTable setBackgroundColor:UIColorFromRGB(BgSystemColor)];
    
    if (iOS7) {
        _listTable.backgroundView = nil;
    }
    
    [_listTable setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    
    MJRefreshBackFooter *footer = [MJRefreshBackFooter  footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)];
    
    _listTable.footer = footer;
    [self setTableSeparatorInset];
    
    
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    
    
    
    MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self loadBegin];
    }];
    header.stateLabel.hidden=YES;
    header.lastUpdatedTimeLabel.hidden=YES;
    _listTable.header = header;
}



-(void)loadBegin{
    page = 0;
    [self loadMoreData];
}

/**
 加载更多
 */
-(void)loadMoreData{
    NSDictionary *p = @{@"session_uid":[UserUtil getUser].user_id,
                        @"nav_type":convertToString(_entity.nav_type),
                        @"page_size":@"20",
                        @"page":convertIntToString(page)
                        };
    NSString *api = API_GetMyFavList;
    
    [NetworkUtil POST:api parameters:p success:^(id responseObject, NSDictionary *dictionary) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        if([self.listTable.header isRefreshing]){
            [self.listTable.header endRefreshing];
        }
        if([self.listTable.footer isRefreshing]){
            [self.listTable.footer endRefreshing];
        }
        if(self->page == 0){
            [self.listArray removeAllObjects];
        }
        NSArray *arr = dictionary[@"data"][@"list"];
        for(NSDictionary *item in arr){
            if([item isKindOfClass:[NSDictionary class]]){
                
                ZZHomeTopicEntity *model = [[ZZHomeTopicEntity alloc] initWithDictionary:item];
                model.nav_type = self->_entity.nav_type;
                model.nav_name = self->_entity.nav_name;
                
                [self.listArray addObject:model];
            }
            
        }
        self.status = (self.listArray.count==0);
        [self.listTable reloadData];
        if(convertToString(dictionary[@"data"][@"page"]).length>0){
            if([dictionary[@"data"][@"page"] intValue] < [dictionary[@"data"][@"pages"] intValue]){
                self->page = [dictionary[@"data"][@"page"] intValue] + 1;
            }
        }
        
    } failure:^(NSError *error, NSString *message) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
//        [PopUtil showMessage:message positionType:PopPositionTypeMiddle];
        if([self.listTable.header isRefreshing]){
            [self.listTable.header endRefreshing];
        }
        if([self.listTable.footer isRefreshing]){
            [self.listTable.footer endRefreshing];
        }
        self.status = (self.listArray.count==0);
        [self.listTable reloadData];
    }];
}


-(void)onCellItemClick:(ZZTopicCellClickType)type obj:(id)senderObj{
    [ZZCoreTools sucaiListenerImpl:senderObj type:type page:self refresh:_listTable];
}


#pragma mark UITableView delegate Start
// 返回section数
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

// 返回section高度
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 0.01f;
}

// 返回section 的View
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *headerView = [[UIView alloc] initWithFrame:CGRectZero];
    return headerView;
}



// 返回section下得行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (self.status) {
        return 1;
    }
    return _listArray.count;
}

// cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (self.status) {
        DefaultCell *cell = [[DefaultCell alloc] init];
        [cell.labText setText:[NSString stringWithFormat:@"暂无%@收藏", _entity.nav_name]];
        return cell;
    }
    
    ZZHomeListCell *cell = (ZZHomeListCell*)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil) {
        cell = [[ZZHomeListCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }
    cell.delegate = self;
    [cell dataToView:[_listArray objectAtIndex:indexPath.row]];
    return cell;
}



// 是否显示删除功能
-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
    return NO;
}

// 删除清理数据
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    editingStyle = UITableViewCellEditingStyleDelete;
}


// table 行的高度
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.status) {
        return ScreenHeight - StatusBarHeight - NavBarHeight;
    }
    //    return 70.0f;
    UITableViewCell *cell = [self tableView:tableView cellForRowAtIndexPath:indexPath];
    return cell.frame.size.height;
}

// table 行的点击事件
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    ZZHomeTopicEntity *model=[_listArray objectAtIndex:indexPath.row];
    ZZContentDetailController *vc = [[ZZContentDetailController alloc] init];
    vc.entity = model;
    [self openPushVC:vc];
    
    
}

//设置分割线间距
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
    if((indexPath.section+1) < _listArray.count){
        UIEdgeInsets inset = UIEdgeInsetsMake(0, 10, 0, 0);
        if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
            [cell setSeparatorInset:inset];
        }
        
        if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
            [cell setLayoutMargins:inset];
        }
    }
}

-(void)viewDidLayoutSubviews{
    [self setTableSeparatorInset];
}
#pragma mark UITableView delegate end

/**
 *  设置UITableView分割线空隙
 */
-(void)setTableSeparatorInset{
    UIEdgeInsets inset = UIEdgeInsetsMake(0, 10, 0, 0);
    if ([_listTable respondsToSelector:@selector(setSeparatorInset:)]) {
        [_listTable setSeparatorInset:inset];
    }
    
    if ([_listTable respondsToSelector:@selector(setLayoutMargins:)]) {
        [_listTable setLayoutMargins:inset];
    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
